package com.sanxiau.dormitory.yang.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.json.JSONException;
import org.json.JSONObject;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class SendSms {
    public static CommonResponse sendSms(String phoneNumbers,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GHaFTFjYzcqsVEmx7ke", "ruNyECmJeZBhevtzBHTbMoZUCRN6nJ");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "ABC商场");
        request.putQueryParameter("TemplateCode", "SMS_202815272");
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String num = "15182945232";
        String code = "123456789";
        CommonResponse response = sendSms(num,code);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(response.getData());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            System.out.println(jsonObject.get("Message"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
